import { random } from 'lodash-es'
import { Random } from 'mockjs'

const explorerMedia = {
  url: '/mock/explorer/media',
  method: 'get',
  statusCode: 200,
  response: (request) => {
    const { itemsPerPage } = request.query
    if (!itemsPerPage) {
      return {
        data: {},
        message: 'itemsPerPage is required',
      }
    }
    return {
      status: 200,
      data: seed(itemsPerPage),
      message: 'ok',
    }
  },
}

const seed = (itemsPerPage) => {
  const cols = [4, 8]
  const data = []
  for (let i = 0; i < itemsPerPage; i++) {
    const bin = random(0, 1)
    data.push({
      cols: cols[bin],
      src: 'https://picsum.photos/200/300',
      duration: random(120, 300),
      watches: random(10000, 30000),
    })
  }
  return data
}

const explorerPurchase = {
  url: '/mock/explorer/purchase',
  method: 'get',
  statusCode: 200,
  response: (request) => {
    const { itemsPerPage } = request.query
    if (!itemsPerPage) {
      return {
        data: {},
        message: 'itemsPerPage is required',
      }
    }
    return seedPurchase(itemsPerPage)
  },
}

const seedPurchase = (itemsPerPage) => {
  const data = []
  for (let i = 0; i < itemsPerPage; i++) {
    data.push({
      purchase_item: Random.ctitle(5, 10),
      order_no: Random.string('number', 2),
      purchase_date: Random.string('number', 2),
      amount: Random.string('number', 4),
      card_num: Random.string('number', 12),
      sub_avatar: `https://cdn01.pinkoi.com/product/aZgvB9di/${Random.string('number', 1)}/800x0.jpg`,
      sub_name: Random.ctitle(2, 8),
      sub_at: Random.ctitle(2, 8),
      sub_post: Random.string('number', 3),
      sub_member: Random.string('number', 4),
    })
  }
  return data
}

export default [explorerMedia, explorerPurchase]
